<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>用户</el-breadcrumb-item>
        <el-breadcrumb-item>premium会员用户阅读数据</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { handleExcelCat } from "@/api/exportCommon";
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appStatisticsBook as baseURL } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      defalut:{
        vip_type : 3
      },
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        selection: true,
        border: true,
        menu: false,
        index:true,
        indexLabel: "序号",
        column: [
         {
            label: "日期",
            prop: "statistics_date_search",
            type: "daterange",
            search: true,
            hide:true
          },
          {
            label: "日期",
            prop: "statistics_date",
            type: "date",
            format:"yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            search: false,
          },
          {
            label: "阅读会员数",
            type: "input",
            prop: "open_reader_num",
          },
          {
            label: "会员用户启动人数",
            prop: "open_app_num",
            type: "input",
          },
          {
            label: "阅读率",
            prop: "avg_read",
            type: "input",
          },
          {
            label: "阅读图书数",
            prop: "read_book_num",
            type: "input",
          },
          {
            label: "阅读章节数",
            prop: "read_chapter_num",
            type: "input",
          },
          {
            label: "阅读时长(分钟)",
            prop: "read_min",
            type: "input",
          },
          {
            label: "人均阅读章节数",
            prop: "avg_read_chapter_num",
            type: "input",
          },
          {
            label: "人均阅读章节数(分钟)",
            prop: "avg_read_book_num",
            type: "input",
          },
        ],
      },
    };
  },
  methods: {
    
    //数据导出
    async handleExcel() {
      this.loading = true;
      //处理搜索条件
      let params = {...this.$refs.crud.searchForm}; 
      if(params.statistics_date_search.length>0){
        params.statistics_date = `${params.statistics_date_search.length>0 ? "!<>!" + params.statistics_date_search[0]+","+ params.statistics_date_search[1] : ""}`
      }
      delete params.statistics_date_search;

      //调用导出接口
      handleExcelCat(
        this.$export,
        baseURL.getVipReadBookList,
        { ...params, ...this.default },
        "会员用户阅读数据",
        this.page.total,
        [...this.option.column]
      );
      this.loading = false;
    },
    /**获取列表 */
    async getList () {
      this.loading = true;
      //清空表格列表
      this.data = [];
      this.params.orderByList = "statistics_date&DESC"
      //发送请求获取数据
      let response = await common(
        `${baseURL.getVipReadBookList}`,
        { ...this.params, ...this.defalut },
        this.page,
        "POST"
      );
      
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange () {
      this.getList();
      
    },
    /**搜索 */
    searchChange (params, done) {
      params.statistics_date = `${params.statistics_date_search ? "!<>!" + params.statistics_date_search : ""}`
      delete params.statistics_date_search;
      this.params = params;
      this.page.currentPage = 1;
      delete params.daterange
      this.getList();
      if(done){done()}
    }
  },
};
</script>